package com.henushang.ranking.controller.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.henushang.ranking.domain.CategoryInfo;
import com.henushang.ranking.domain.RankingData;
import com.henushang.ranking.domain.RankingDataResponseVo;
import com.henushang.ranking.domain.WebsiteInfo;
import com.henushang.ranking.service.CategoryService;
import com.henushang.ranking.service.RankingDataService;
import com.henushang.ranking.util.JSONUtil;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RankingDataService rankingDataService;

    @RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET)
    public String index(Model model) {
        List<CategoryInfo> navCates = categoryService.getNavCategory();
        model.addAttribute("nav_cate", navCates);
        Map<CategoryInfo, Map<WebsiteInfo, List<RankingData>>> cateRankingDataMap = rankingDataService
                .getRankingDataByCategory();
        model.addAttribute("cateRankMap", cateRankingDataMap);
        return "/index";
    }

    @RequestMapping(value = "/ajax/index/{cateId}", method = RequestMethod.GET)
    public String getDataByCateId(Model model, @PathVariable String cateId) {
        long s1 = System.currentTimeMillis();
        Map<WebsiteInfo, List<RankingData>> rankData = rankingDataService
                .getRankingDataByCategory(cateId);
        long s2 = System.currentTimeMillis();
        System.out.println("time:" + (s2 - s1));
        model.addAttribute("rank_data", rankData);

        CategoryInfo cateInfo = categoryService.getById(cateId);
        model.addAttribute("cate_info", cateInfo);

        return "ajax_site_rank";
    }
}
